package net.zjitc.servlet;

import net.zjitc.domain.User;
import net.zjitc.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/9/27.
 */
@WebServlet(name = "LoginServlet",urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("user");
        String password = request.getParameter("pass");
        //service 业务层，表示示层应该从业务层取数据  服务员---->厨师
        UserService userService = new UserService();
        User user = null;
        try {
            user = userService.findByUsernameAndPassword(username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (user != null){
           //跳转到成功界面，重定向使用的地址为URI,
            response.sendRedirect("/login/success.jsp");
        }else {
            //跳转到登录界面
            response.sendRedirect("/login/login.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
